package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.MachineFunction;
import com.tbit.main.service.DataService;
import com.tbit.main.service.MachineFunctionService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 设备功能配置
 *
 * @author LMY
 * @create 2022-07-05 14:07
 */
@RestController
@RequestMapping("/machineFunction")
public class MachineFunctionController {

    @Autowired
    private MachineFunctionService machineFunctionService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private DataService dataService;

    /**
     * 批量添加
     *
     * @param token
     * @param machineNO    设备编号
     * @param functionType 功能类型列表 （只传打开的功能列表）
     * @return null
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.MACHINEFUNCTION, methods = MethodEnum.MACHINEFUNCTION_ADD)
    public JsonResult add(@NotNull String token, @NotNull String machineNO,
                          @RequestParam(value = "functionType", required = false) List<Integer> functionType,
                          Integer functionMode

    ) {
        Assert.notNull(machineNO, "Machine.machineNO.notNull");

        AccountUser accountuser = tokenService.getAccount(token);

        /**设备权限判断*/
        machineService.auth(accountuser.getAgents(), dataService.getMachineId(machineNO));
        machineFunctionService.add(machineNO, functionType, 1, functionMode);

        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param token
     * @param machineNO    设备编号
     * @param functionType 功能类型
     * @return null
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.MACHINEFUNCTION, methods = MethodEnum.MACHINEFUNCTION_DEL)
    public JsonResult delBatch(String token, String machineNO, Integer functionType) {
        Assert.notNull(machineNO, "Machine.machineNO.notNull");
        Assert.notNull(functionType, "functionType.notNull");

        AccountUser accountUser = tokenService.getAccount(token);
        /**设备权限判断*/
        machineService.auth(accountUser.getAgents(), dataService.getMachineId(machineNO));
        machineFunctionService.del(machineNO, functionType);
        return JsonResult.succ();
    }

    /**
     * 根据车辆查询设备头盔功能
     *
     * @param token
     * @param userCode
     * @return
     */
    @RequestMapping("/getHelmetType")
    public JsonResult<MachineFunction> getHelmetType(String token, String userCode) {
        Assert.notNull(userCode, "Machine.userCode.notNull");

        AccountUser accountUser = tokenService.getAccount(token);
        /**设备权限判断*/
        machineService.auth(accountUser.getAgents(), dataService.getMachineIdUserCode(userCode));

        MachineFunction machineFunction = machineFunctionService.getMachineIdHelmet(dataService.getMachineIdUserCode(userCode));

        return JsonResult.succ(machineFunction);
    }

    /**
     * 根据车辆查询功能
     *
     * @param token
     * @param userCode
     * @return
     */
    @RequestMapping("/getType")
    public JsonResult<List<MachineFunction>> getType(String token, String userCode) {
        Assert.notNull(userCode, "Machine.userCode.notNull");

        AccountUser accountUser = tokenService.getAccount(token);
        /**设备权限判断*/
        machineService.auth(accountUser.getAgents(), dataService.getMachineIdUserCode(userCode));

        List<MachineFunction> machineFunction = machineFunctionService.getMachineId(dataService.getMachineIdUserCode(userCode));

        return JsonResult.succ(machineFunction);
    }
}
